let arr1:string = '111'
console.log(arr1);

if(arr1) {
  console.log(arr1 + typeof(arr1));
}

// 必须给一个泛型, 如果是数组的话.
let arr: Array<number> = [1,2, 3, 4]
console.log(arr);

// 如果使用的是数组, 可以这样定义的.
let arr2: [number, string] = [1, 'qqqq']
console.log(arr2);

// 在定义变量的时候, 就要给一个类型, 因为ts有类型检测机制
let arr3 : Array<string> = ['1', '2', '3']
console.log(arr3);

// 组合字符串, 拼接字符串
let name1:string = 'wangbo'
console.log(`hello world ${name1}`);

// 拼接字符串
let name2:string = 'wangbo111'
console.log('你好呀'+name2+'我爱你呀, 你要好好地');

// 函数返回值可以是 number
let add = function(num1:number, num2:number):number {
  return num1 + num2
}

console.log(add(1, 3));

// 返回的是无返回值的
let voidFun = function(num1:number, num2:number):void {
  console.log('哈哈哈哈哈');
  return
}

voidFun(1, 5)

// void的变量没有什么用, 只能赋值为undefined 和 null
let unused:void = undefined
unused = null
// 只能声明为undefined 和 null.
// unused = 'hello'

//  任意值
let anyPerson:any = 'hello'
// anyPerson.setName('bob')
console.log(anyPerson);

// 任意值 , 给any值后面不会报错的.
let myFavoriteNumber:any = 12;
// 他会自己类型推断的.
// 下面的会报错的.
myFavoriteNumber = '12'


// 还可以联合声明
let unionStr : string | number = 12;
unionStr = '242424'
console.log(unionStr);
console.log(typeof unionStr);

// 基本类型
// boolean、string、number、undefined、null
// symbol、promise
// object、array、function
let functionType = function(arr1:Array<number>) {
  console.log(arr1.length);
}
functionType([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

// 接口的实现
interface person {
  name:string,
  age: number
  addr: string
}

// 接口, 多一个少一个都不行的.
let tom: person = {
  name: 'bob',
  age: 12,
  addr: '安徽省宣城市宁国市港口镇新时代小区'
}

// 返回的数值
console.log(tom.name+ '\n'+tom.age+'\n'+tom.addr);
console.log('姓名:'+tom.name+'\n'+'年纪:'+tom.age+'\n'+'地址:'+tom.addr);

// 接口的制定, 可以使用 可选属性, 但是不可以添加属性.
interface animal {
  type : string,
  color: string,
  // @ts-ignore
  age?: number
}

let kate: { color: string; type: string } = {
  type: 'cat',
  color: 'white & black'
}

// 定义的对象可以访问
console.log(kate);
console.log(tom);

// 接口是不可以访问的
// console.log(person);
// console.log(animal);


interface fruits {
  id: number,
  name: string,
  add: string,
  comeFrom?:string
}

let apple:fruits = {
  id: 1,
  name: 'apple',
  add: '安徽',
  comeFrom: 'mercian'
}

console.log(apple.comeFrom);
console.log(apple.name);

// 定义数组
let arr4:number[] = [1,2,3,4,5]
let arr5 = arr4.filter(n => n>4)
console.log(arr5);
console.log(arr4.filter(n => n>3));


// 根据id, 删除数据操作.
let id:number = 1
let type:string = '香蕉'
let arr6:Array<string> = ['苹果', '香蕉', '葡萄', '梨']

let chooseReduce = function(obj:Array<string>, type?:string, id?:number) {
  if(id) {
    for(let i of arr6) {
      if(Number(i) === id) {
        obj =  obj.splice(id)
      }
    }
  }
  if(type) {
    obj =  obj.filter( n => n !== type)
  }
  return obj
}

const result = chooseReduce(arr6, type, id)
console.log(result);

// 剩余参数
function push1(array, ...items):void {
  console.log(array);
  console.log(items);
}

let array1 = [1, 2, 3, 4, 5]
let x:number  = 10
let y:number  = 12

push1(array1, x, y)

function arrPush( arr: any[], ...rest:any[]){
  rest.forEach(n => {
    arr.push(rest)
  })
  return arr
}

let arr8 = [1, 2, 3, 4, 5]
const result8 = arrPush(arr8, [1,2,3,4])
console.log(result8);


// 检测一个对象的类型
interface Cat {
  name: string,
  run():void,
}
interface Fish {
  name: string,
  swim():void,
}


function isFish(animal:Cat | Fish) {
  // 使用断言,as 关键字
  if(typeof (animal as Fish).swim === 'function'){
    return true
  }
  return false
}

let fish1:Fish = {
  name: 'bob',
  swim(){
    console.log('swim');
  }
}
const result9 = isFish(fish1)
console.log(result9);

// 自定义类型
type EventName = 'click' | 'scroll'|'mousemove'

function handle(ele: Element, event: EventName){
  if(ele && event) {
    // 对DOM元素进行、event的方法调用, 进行原生的dom操作.
    this.$listeners(ele, event)
  }
}


// 元祖
let arr10: [number, string] = [11, 'bob'];

arr[0].toFixed(3)
console.log(arr[0]);


//  元祖后来push进去的数据, 数据类型是元祖中数据的结合.
let arr11 : [string,  number, number] = ['bob', 11, 12]
arr11.push(111)
arr11.push('安徽')
console.log(arr11);
console.log(typeof arr11[0]);
console.log(arr11.length);
console.log(typeof arr11[2]);

// 枚举类型
enum num {one, two, three, four, five}
// 可以通过key和value 分别对枚举进行遍历.
console.log(num[0] ==='one');
console.log(num['one'] === 0);
console.log(num);


// 枚举类型中添加其他数值项, 枚举中的数值是无法被改变的, 改变了就会报错了;
enum Color {red, blue, white}
console.log(Color[2]);
console.log(Color[0]);
console.log(Color[1]);
console.log('white'.length);


console.log('this is the first hotUpdate programe');
console.log('this is the first hotUpdate programe');

function add111 (num1:number): number {
  console.log('值为%s',num1 * 2);

  return num1 + num1
}

add111(1)































